﻿using _2._18._2015_MoviesInClass.Data;
using _2._18._2015_MoviesInClass.Data.Models;
using _2._18._2015_MoviesInClass.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _2._18._2015_MoviesInClass.Adapters
{
    public class MovieDataAdapter : IMovieAdapter
    {
        public string MakePeanutButter()
        {
            return "mmmm good peanut butter";
        }

        public List<MovieViewModel> GetMovies(string userId = null)
        {
            List<MovieViewModel> models = null;

            using (MoviesDbContext db = new MoviesDbContext())
            {
                var q = db.Movies.AsQueryable();

                if (userId != null)
                    q = q.Where(m => m.UserId == userId);

                models = q.Select(m => new MovieViewModel
                {
                    MovieId = m.MovieId,
                    PosterUrl = m.PosterUrl,
                    Rating = m.Rating,
                    Title = m.Title
                }).ToList();
            }

            return models;
        }

        public void CreateMovie(MovieViewModel model, string userId)
        {
            using (MoviesDbContext db = new MoviesDbContext())
            {
                Movie dbMovie = new Movie
                {
                    PosterUrl = model.PosterUrl,
                    Rating = model.Rating,
                    Title = model.Title,
                    UserId = userId
                };

                db.Movies.Add(dbMovie);
                db.SaveChanges();
            }
        }

        public MovieViewModel GetMovie(int movieId)
        {
            throw new NotImplementedException();
        }

        public void UpdateMovie(MovieViewModel model)
        {
            throw new NotImplementedException();
        }
    }
}